package com.atguigu.springcloud.OrderMain80.controller;

import com.atguigu.springcloud.OrderMain80.lb.LoadBalancer;
import com.atguigu.springcloud.OrderMain80.service.PaymentClient;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * @Author: 王丹
 * @Date: 2021/2/27 0027 25
 * @Description:
 */
@RestController
@Slf4j
public class OrderController {

    //public static final String PATH_URL = "http://localhost:8001";单机版地址写死没问题，但是集群不行，要使用微服务名称

    public static final String PATH_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private PaymentClient paymentClient;

    @Resource
    private DiscoveryClient discoveryClient;

    @Resource
    private LoadBalancer loadBalancer;

    /*@GetMapping(value = "/consumer/create")
    public CommonResult<Payment> create(Payment payment) {
        return restTemplate.postForObject(PATH_URL + "/payment/create",payment,CommonResult.class);
    }*/
    @GetMapping("/consumer/create")
    public CommonResult<Payment> create(Payment payment) {
        return restTemplate.postForObject(PATH_URL + "/payment/create", payment, CommonResult.class);  //写操作
    }

    @GetMapping("/consumer/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") long id) {
        return restTemplate.getForObject(PATH_URL + "/payment/get/" + id, CommonResult.class);
    }

    //使用restTemplate的getForEntity和postForEntity
    @GetMapping("/consumer/createEntity")
    public CommonResult<Payment> createEntity(Payment payment) {
        return restTemplate.postForEntity(PATH_URL + "/payment/create", payment, CommonResult.class).getBody();  //写操作
    }

    @GetMapping("/consumer/getForEntity/{id}")
    public CommonResult<Payment> getPaymentEntity(@PathVariable("id") long id) {
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PATH_URL + "/payment/get/" + id, CommonResult.class);
        if (entity.getStatusCode().is2xxSuccessful()) {
            log.info(entity.getStatusCode() + "\t" + entity.getHeaders());
            return entity.getBody();
        } else {
            return new CommonResult<>(444, "操作失败");
        }
    }

    @GetMapping("/consumer/payment/lb")
    public String getPaymentLB() {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances == null || instances.size() <= 0) {
            return null;
        }
        ServiceInstance serviceInstance = loadBalancer.instance(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri + "/payment/lb", String.class);
    }


    /*//使用feign就打开该段代码
    @GetMapping("/consumer/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") long id) {
        //return restTemplate.getForObject(PATH_URL + "/payment/get/" + id, CommonResult.class);
        System.out.println("使用feign");
        return paymentClient.getPaymentById(id);
    }*/
}
